package com.breaking.second.utils.compat;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;

import java.util.Set;

import okhttp3.FormBody;

public class FormBodyCompat {
    public static FormBody.Builder to(Object jsonObject) {
        return to(JSON.toJSONString(jsonObject));
    }

    public static FormBody.Builder to(String jsonObject) {
        return to(JSONObject.parse(jsonObject));
    }

    public static FormBody.Builder to(JSONObject jsonObject) {
        FormBody.Builder builder = new FormBody.Builder();
        Set<String> keys = jsonObject.keySet();
        for (String key : keys) {
            String value = jsonObject.getString(key);
            if (value == null) continue;
            builder.add(key, value);
        }
        return builder;
    }

    public static void inst(FormBody.Builder form, Object o) {
        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(o));
        Set<String> keys = jsonObject.keySet();
        for (String key : keys) {
            String value = jsonObject.getString(key);
            if (value == null) continue;
            form.add(key, value);
        }
    }
}
